<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="admin/public/head::head(~{::title},~{},~{})">
    <title>添加文件-新闻管理系统</title>
</head>

<body>
<style type="text/css">
    .layui-form-item {
        margin-bottom: 30px;
        clear: both;
        zoom: 1;
    }
    .layui-form-item .layui-input-inline{
        margin-right: 50px;
    }
    .my-two-form{
        padding-left: 10%;
    }
</style>
<div class="weadmin-body">
    <form class="layui-form layui-form-pane my-two-form" id="form" enctype="multipart/form-data">
        <div class="layui-form-item">
            <label for="field0" class="layui-form-label">
                <span class="we-red">*</span>文件主题
            </label>
            <div class="layui-input-inline" style="width: 80%">
                <input type="text" id="field0" name="field0" required lay-verify="required"
                       placeholder="文件主题" autocomplete="off"  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="categoryCode" class="layui-form-label">
                <span class="we-red">*</span>文件类目
            </label>
            <div class="layui-input-inline" style="width: 80%">
                <select  name="categoryCode" id="categoryCode" th:placeholder="文件分类">
                    <option  th:each="category:${categoryList}" th:value="${category.dictValue}">[[${category.name}]]</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">上传文件</label>
            <div class="layui-input-inline" style="margin-right: 0px">
                <button type="button" onclick="clearImg();" class="layui-btn" id="uploadimg">
                    <i class="layui-icon">&#xe67c;</i>选择文件
                </button>
            </div>
            <div class="layui-input-inline" style="margin-right: 0px;width: 100%" id="imgBox"></div>
        </div>
        <div class="layui-form-item">
            <button style="margin-left:25%;width: 200px" class="layui-btn" lay-filter="add" lay-submit>添加</button>
        </div>
    </form>
</div>
<script type="text/javascript">
    layui.use(['form', 'layer','upload', 'jquery', 'admin'], function () {
        var form = layui.form,
            $ = layui.jquery,
            upload = layui.upload,
            layer = layui.layer;


        //文件上传
        var uploadInst = upload.render({
            elem: '#uploadimg'
            , multiple: true//多图上传，不支持IE8/9
            , field: 'file'//后台接收字段名
            , auto: false//不自动上传
            , accept: 'file'//（可以上传所有）
            , acceptMime: 'file'//（可以上传所有）
            // , acceptMime: 'image/*'//（只显示文件文件）
            , choose: function (obj) { //选择文件后执行
                //预读本地文件，如果是多文件，则会遍历。(不支持ie8/9)
                $("#imgBox").empty();
                obj.preview(function (index, file, result) {
                    console.log(index);
                    console.log(file);
                    console.log(result);
                    var reg=/^image.*$/g
                    if(reg.test(file.type)){
                        var img = "<img width='100px' height='100px' src='" + result + "'alt='' class='img-rounded'>";
                        $("#imgBox").append(img);
                    }else{
                        $("#imgBox").html(file.name);
                    }

                });
            }
        });
        //监听提交
        form.on('submit(add)', function (data) {
            console.log(data.field);
            var formData = new FormData($("#form")[0]);
            if (data.field.file == null || data.field.file == "") {
                layer.msg("请上传文件！", {icon: 2, time: 1500});
                return false;
            }
            $.ajax({
                url: "/file/add",
                type: "post",
                data: formData,
                processData: false,
                contentType: false,
                beforeSend: function(){
                    //返回的参数item，即为当前的input DOM对象
                    index = layer.load(1,{shade: [0.3,'grey']});
                },
                success: function (res) {
                    //关闭遮罩层
                    layer.close(index);
                    if (res.success == true) {
                        layer.msg(res.msg, {icon: 1, time: 1500},function () {
                            refreshParent();
                        });
                    } else {
                        layer.msg(res.msg, {icon: 2, time: 1500});
                    }
                },
                error: function (res) {
                    layer.msg(res.msg, {icon: 2, time: 1500});
                },
                complete: function () {
                }
            });
            return false;
        });
    });
    /*清除回显图片*/
    function clearImg() {
        // $("#imgBox").empty();
    }
</script>
</body>

</html>